package com.banciyuan.bcywebview.biz.post.auto;

import android.content.Context;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by squall on 2016/1/6.
 */
public class AutoComplateHelper {

    private Context mContext;
    private RequestQueue mQueue;
    private IGetAutoComplate iGetAutoComplate;
    private String type = "work";

    public AutoComplateHelper(Context mContext, IGetAutoComplate iGetAutoComplate, String type) {
        this.mContext = mContext;
        this.mQueue = VolleyQueue.getRquest(mContext);
        this.iGetAutoComplate = iGetAutoComplate;
        this.type = type;
    }

    public void findServerTitle(String editable, final boolean show, final String url) {
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        paramsList.add(new BcyPair(HttpUtils.SEARCH_QUERY, editable.trim()));
        paramsList.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        if (!TextUtils.isEmpty(type)) {
            paramsList.add(new BcyPair(HttpUtils.TYPE, type));
        }
        final HashMap<String, String> map = HttpUtils.getData(paramsList);

        new AsyncTask<String, String, String>() {
            @Override
            protected String doInBackground(String... strings) {
                String temp = strings[0];
                for (Map.Entry<String, String> entry : map.entrySet())
                    temp += entry.getKey() + "=" + entry.getValue();
                if (mQueue.getCache().get(temp) != null) {
                    return new String(mQueue.getCache().get(temp).data, Charset.defaultCharset());
                } else {
                    return "net";
                }
            }

            @Override
            protected void onPostExecute(final String s) {
                if (s.equals("net")) {
                    Response.Listener<String> onSucc = new Response.Listener<String>() {
                        @Override
                        public void onResponse(String o) {
                            iGetAutoComplate.getComplateList(o, show);
                        }
                    };
                    Response.ErrorListener onError = new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            iGetAutoComplate.getComplateList(s, show);
                        }
                    };

                    OnErrorProxy errorProxy = new OnErrorProxy(onError, onSucc, url, mContext, map);
                    StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, errorProxy);
                    mQueue.add(stringRequestParam);

                } else {
                    iGetAutoComplate.getComplateList(s, show);
                }
            }
        }.execute(url);
    }


    public void findServerTitle(Editable editable, final boolean show, final String url) {
        findServerTitle(editable.toString(), show, url);
    }
}
